package top.lilele.adminSystem.web;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.lilele.adminSystem.dto.LoginDto;
import top.lilele.adminSystem.dao.UserListInfoDao;
import top.lilele.adminSystem.dto.*;
import top.lilele.adminSystem.entity.AdminUserEntity;
import top.lilele.adminSystem.exception.account.CodeErrorException;
import top.lilele.adminSystem.exception.account.CodeOutTimeException;
import top.lilele.adminSystem.exception.account.PasswordNotEqualsException;
import top.lilele.adminSystem.mapper.AdminUserMapper;
import top.lilele.adminSystem.service.AdminUserService;
import top.lilele.adminSystem.utils.BaseResponse;
import top.lilele.adminSystem.utils.RedisUtil;
import top.lilele.adminSystem.utils.ResponseCodeEnum;
import top.lilele.adminSystem.validGroup.AddUserGroup;
import top.lilele.adminSystem.validGroup.UpdateUserGroup;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * @author lilele
 */
@RestController
@RequestMapping(value = "user")
@Slf4j
@Api(tags = "用户管理")
public class AdminUserController {
    @Resource
    AdminUserMapper adminUserMapper;

    @Resource
    AdminUserService adminUserService;

    @Resource
    RedisUtil redisUtil;

    /**
     * 登录
     *
     * @param loginDto
     * @return
     */
    @ApiOperation(value = "登录")
    @SneakyThrows
    @PostMapping(value = "login")
    public BaseResponse login(@Validated @RequestBody LoginDto loginDto) {
        String code = (String) redisUtil.get(loginDto.getUid());
        if (code == null) {
            throw new CodeOutTimeException("验证码到期");
        }
        if (!code.equals(loginDto.getCode())) {
            throw new CodeErrorException("验证码错误");
        }
        String token = adminUserService.login(loginDto);
        return BaseResponse.info(ResponseCodeEnum.Success, token);
    }

    /**
     * 新增用户
     *
     * @param addUserDto
     * @return
     */
    @ApiOperation(value = "添加用户")
    @SneakyThrows
    @PostMapping(value = "add")
    public BaseResponse add(@Validated(value = {AddUserGroup.class}) @RequestBody AddUserDto addUserDto) {
        adminUserService.addUser(addUserDto);
        return BaseResponse.info(ResponseCodeEnum.Success, null);
    }

    @ApiOperation(value = "根据id删除用户")
    @PostMapping(value = "deleteUserById")
    public BaseResponse deleteUserById(@Validated @RequestBody IdParams idParams) {
        adminUserService.deleteUserById(idParams.getId());
        return BaseResponse.info(ResponseCodeEnum.Success, null);
    }

    /**
     * 根据id更新用户
     *
     * @param addUserDto
     * @return
     */
    @ApiOperation(value = "根据id更新用户")
    @PostMapping(value = "updateUserInfoById")
    public BaseResponse updateUserInfoById(@Validated({UpdateUserGroup.class}) @RequestBody AddUserDto addUserDto) {
        adminUserService.updateUserInfoById(addUserDto);
        return BaseResponse.info(ResponseCodeEnum.Success, null);
    }

    /**
     * 根据id查询用户
     *
     * @param idParams
     * @return
     */
    @ApiOperation(value = "根据id查询用户")
    @PostMapping(value = "queryUserInfoById")
    public BaseResponse queryUserInfoById(@RequestBody IdParams idParams) {
        UserListDto userListDto = adminUserService.queryUserInfoById(idParams.getId());
        return BaseResponse.info(ResponseCodeEnum.Success, userListDto);
    }

    /**
     * 注销账号
     *
     * @return
     */
    @ApiOperation(value = "注销账号")
    @PostMapping(value = "logout")
    public BaseResponse logout() {
        StpUtil.logout();
        return BaseResponse.info(ResponseCodeEnum.Success, null);
    }

    /**
     * 踢出下线
     *
     * @param idParams
     * @return
     */
    @ApiOperation(value = "强制下线")
    @PostMapping(value = "logoutByLoginId")
    public BaseResponse logoutByLoginId(@Validated @RequestBody IdParams idParams) {
        StpUtil.logout(idParams.getId());
        return BaseResponse.info(ResponseCodeEnum.Success, null);
    }

    /**
     * 查询所有用户
     *
     * @param userListInfoDao
     * @return
     */
    @ApiOperation(value = "查询用户列表")
    @PostMapping(value = "queryUserList")
    public BaseResponse queryUserList(@Validated @RequestBody UserListInfoDao userListInfoDao) {
        return BaseResponse.info(ResponseCodeEnum.Success, adminUserService.queryUserList(userListInfoDao, true));
    }

    /**
     * 冻结账号
     *
     * @param idParams
     * @return
     */
    @ApiOperation(value = "冻结账号")
    @PostMapping(value = "disAbleAccount")
    public BaseResponse disAbleAccount(@RequestBody IdParams idParams) {
        StpUtil.logout(idParams.getId());
        UpdateWrapper<AdminUserEntity> updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", idParams.getId()).set("available", 2);
        adminUserMapper.update(null, updateWrapper);
        return BaseResponse.info(ResponseCodeEnum.Success, null);
    }

    /**
     * 解封账号
     *
     * @param idParams
     * @return
     */
    @ApiOperation(value = "解封账号")
    @PostMapping(value = "enAbleAccount")
    public BaseResponse enAbleAccount(@RequestBody IdParams idParams) {
        UpdateWrapper<AdminUserEntity> updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", idParams.getId()).set("available", 1);
        adminUserMapper.update(null, updateWrapper);
        return BaseResponse.info(ResponseCodeEnum.Success, null);
    }

    /**
     * 根据用户id获取用户信息
     *
     * @return
     */
    @PostMapping(value = "queryUserById")
    public BaseResponse queryUserById() {
        return BaseResponse.info(ResponseCodeEnum.Success, adminUserService.queryUserById(StpUtil.getLoginIdAsString()));
    }


    /**
     * 修改密码
     *
     * @param changePasswordDto
     * @return
     */
    @ApiOperation(value = "修改密码")
    @SneakyThrows
    @PostMapping(value = "changePassword")
    public BaseResponse changePassword(@RequestBody @Validated ChangePasswordDto changePasswordDto) {
        // 两次密码不相同校验
        if (!changePasswordDto.getFinalPassword().equals(changePasswordDto.getSurePassword())) {
            throw new PasswordNotEqualsException();
        }
        adminUserService.changePassword(changePasswordDto);
        return BaseResponse.info(ResponseCodeEnum.Success, null);
    }

    @ApiOperation(value = "导出用户列表报表")
    @PostMapping(value = "export")
    public void export(@RequestBody UserListInfoDao userListInfoDao, HttpServletResponse httpServletResponse) {
        adminUserService.export(userListInfoDao, httpServletResponse);
    }

    /**
     * 查询部门-成员树
     *
     * @return
     */
    @ApiOperation(value = "查询部门-成员树")
    @PostMapping(value = "selectAllDeptAndUser")
    public BaseResponse selectAllDeptAndUser() {
        List<DeptUserDao> list = adminUserService.selectAllDeptAndUser();
        return BaseResponse.info(ResponseCodeEnum.Success, list);
    }

    /**
     * 生成登录二维码
     *
     * @param httpServletResponse
     */
    @ApiOperation(value = "生成登录二维码")
    @GetMapping(value = "generateCode")
    public void generateCode(HttpServletResponse httpServletResponse) {
        adminUserService.generateCode(httpServletResponse);
    }
}
